class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        auto sum = 0;
        for (auto x : nums) sum += x;
        if (sum + target < 0) return 0;
        if ((sum + target) & 1) return 0;
        int left = (sum + target) >> 1;
        vector<int> dp(left + 1);
        dp[0] = 1;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = left; j >= nums[i]; j--) {
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[left];
    }
};
